var app = getApp();
var util = require('../../utils/util.js');  
Page({
  data: {
    dateValue: "请选择日期",
    location:"请选择",
    county:"请选择",
    objectiveItemid:1,
    seatimg:[
    {
      id: 0,
      img: 'http://images.globalwings.com/content/flightImg/Facon7X/chair/chair.png',
      imglist: ['http://images.globalwings.com/content/flightImg/Facon7X/chair/chair.png','http://images.globalwings.com/content/flightImg/Facon7X/chair/chair-check.png'],
      top:'12px',
      left: '182px',
      width: '85px',
      height: '98px',
      flag: false
    },
    {
      id: 1,
      img: 'http://images.globalwings.com/content/flightImg/Facon7X/chair/chairdown.png',
      imglist: ['http://images.globalwings.com/content/flightImg/Facon7X/chair/chairdown.png','http://images.globalwings.com/content/flightImg/Facon7X/chair/chairdown-check.png'],
      top:'201px',
      left: '182px',
      width: '85px',
      height: '98px',
      flag: false
    },
    {
      id: 2,
      img: 'http://images.globalwings.com/content/flightImg/Facon7X/chair/chair.png',
      imglist: ['http://images.globalwings.com/content/flightImg/Facon7X/chair/chair.png','http://images.globalwings.com/content/flightImg/Facon7X/chair/chair-check.png'],
      top:'12px',
      left: '35px',
      width: '85px',
      height: '98px',
      flag: false
    },
    {
      id: 3,
      img: 'http://images.globalwings.com/content/flightImg/Facon7X/chair/chairdown.png',
      imglist: ['http://images.globalwings.com/content/flightImg/Facon7X/chair/chairdown.png','http://images.globalwings.com/content/flightImg/Facon7X/chair/chairdown-check.png'],
      top:'201px',
      left: '35px',
      width: '85px',
      height: '98px',
      flag: false
    },
    {
      id: 4,
      img: 'http://images.globalwings.com/content/flightImg/Facon7X/chair/smallchair.png',
      imglist: ['http://images.globalwings.com/content/flightImg/Facon7X/chair/smallchair.png','http://images.globalwings.com/content/flightImg/Facon7X/chair/smallchair-check.png'],
      top:'339px',
      left: '85px',
      width: '62px',
      height: '89px',
      flag: false
    },
    {
      id: 5,
      img: 'http://images.globalwings.com/content/flightImg/Facon7X/chair/smallchair.png',
      imglist: ['http://images.globalwings.com/content/flightImg/Facon7X/chair/smallchair.png','http://images.globalwings.com/content/flightImg/Facon7X/chair/smallchair-check.png'],
      top:'339px',
      left: '24px',
      width: '62px',
      height: '89px',
      flag: false
    },
    {
      id: 6,
      img: 'http://images.globalwings.com/content/flightImg/Facon7X/chair/smallchairdown.png',
      imglist: ['http://images.globalwings.com/content/flightImg/Facon7X/chair/smallchairdown.png','http://images.globalwings.com/content/flightImg/Facon7X/chair/smallchairdown-check.png'],
      top:'525px',
      left: '85px',
      width: '62px',
      height: '89px',
      flag: false
    },
    {
      id: 7,
      img: 'http://images.globalwings.com/content/flightImg/Facon7X/chair/smallchairdown.png',
      imglist: ['http://images.globalwings.com/content/flightImg/Facon7X/chair/smallchairdown.png','http://images.globalwings.com/content/flightImg/Facon7X/chair/smallchairdown-check.png'],
      top:'525px',
      left: '24px',
      width: '62px',
      height: '89px',
      flag: false
    },
    {
      id: 8,
      img: 'http://images.globalwings.com/content/flightImg/Facon7X/chair/rightup.png',
      imglist: ['http://images.globalwings.com/content/flightImg/Facon7X/chair/rightup.png','http://images.globalwings.com/content/flightImg/Facon7X/chair/rightup-check.png'],
      top:'644px',
      left: '178px',
      width: '89px',
      height: '96px',
      flag: false
    },
    {
      id: 9,
      img: 'http://images.globalwings.com/content/flightImg/Facon7X/chair/rightmd.png',
      imglist: ['http://images.globalwings.com/content/flightImg/Facon7X/chair/rightmd.png','http://images.globalwings.com/content/flightImg/Facon7X/chair/rightmd-check.png'],
      top:'741px',
      left: '178px',
      width: '89px',
      height: '80px',
      flag: false
    },
    {
      id: 10,
      img: 'http://images.globalwings.com/content/flightImg/Facon7X/chair/rightdn.png',
      imglist: ['http://images.globalwings.com/content/flightImg/Facon7X/chair/rightdn.png','http://images.globalwings.com/content/flightImg/Facon7X/chair/rightdn-check.png'],
      top:'822px',
      left: '178px',
      width: '89px',
      height: '95px',
      flag: false
    },
    {
      id: 11,
      img: 'http://images.globalwings.com/content/flightImg/Facon7X/chair/leftup.png',
      imglist: ['http://images.globalwings.com/content/flightImg/Facon7X/chair/leftup.png','http://images.globalwings.com/content/flightImg/Facon7X/chair/leftup-check.png'],
      top:'644px',
      left: '28px',
      width: '89px',
      height: '96px',
      flag: false
    },
    {
      id: 12,
      img: 'http://images.globalwings.com/content/flightImg/Facon7X/chair/leftmd.png',
      imglist: ['http://images.globalwings.com/content/flightImg/Facon7X/chair/leftmd.png','http://images.globalwings.com/content/flightImg/Facon7X/chair/leftmd-check.png'],
      top:'741px',
      left: '28px',
      width: '89px',
      height: '80px',
      flag: false
    },
    {
      id: 13,
      img: 'http://images.globalwings.com/content/flightImg/Facon7X/chair/leftdn.png',
      imglist: ['http://images.globalwings.com/content/flightImg/Facon7X/chair/leftdn.png','http://images.globalwings.com/content/flightImg/Facon7X/chair/leftdn-check.png'],
      top:'822px',
      left: '28px',
      width: '89px',
      height: '95px',
      flag: false
    }
    ],
    // 单价
    singleprice: 10000,
    // 总价格
    zotalprice: 0,
    // 总个数
    lenarr: 0,
    jiweiNum: [],
    objectiveItem:[
      {
        "id":1,
        "name":"商务",
      },
      {
        "id": 2,
        "name": "旅行",
      },
      {
        "id": 3,
        "name": "医疗",
      },
      {
        "id": 4,
        "name": "度假",
      }
    ]
  },

  onShow() { //返回显示页面状态函数
    let that = this;
    if (app.globalDatainfo.location) {
      that.location = app.globalDatainfo.location;
        that.setData({
        location: app.globalDatainfo.location
      })
    }

    if (app.globalDatainfo.county) {
      that.county = app.globalDatainfo.county;
      that.setData({
        county: app.globalDatainfo.county
      })
    }
  },
  // 数组去重
  uniq: function(array){
      var temp = []; //一个新的临时数组
      for(var i = 0; i < array.length; i++){
          if(temp.indexOf(array[i]) == -1){
              temp.push(array[i]);
          }
      }
      return temp;
  },
  onLoad: function (options) {
    var time = util.formatDate(new Date());
    this.objectiveItemid=1;
    wx.getStorage({//获取本地缓存
      key:"jiweiNum",
      success:(res)=>{
        var dataarr = this.uniq(res.data);
        this.setData({
          jiweiNum:res.data
        });
        var arr = this.data.jiweiNum; 
        console.log(arr);
        var len = arr.length;
        var singleprice = this.data.singleprice;
        var zotalprice = singleprice*len;
        arr.forEach((item,index,arr)=>{
          var item = item - 1;
          var img = 'seatimg['+item+'].img';
          var imglist0 = this.data.seatimg[item].imglist[0];
          var imglist1 = this.data.seatimg[item].imglist[1];
          var flags = 'seatimg['+item+'].flag';
          this.setData({
            [img]: imglist1,
            zotalprice: zotalprice,
            lenarr: len,
            [flags]: true
          })
        })
      }
    });

    
  },
  // 选择座位的操作
  chooseSeat: function (e) {
    var that = this;
    // 当前元素的下标
    var index = e.currentTarget.dataset.index;
    var flag = e.currentTarget.dataset.flag;
    var setflag = 'seatimg['+index+'].flag';
    // 当前图片的地址
    var img = 'seatimg['+index+'].img';
    var imglist0 = that.data.seatimg[index].imglist[0];
    var imglist1 = that.data.seatimg[index].imglist[1];
    var num = 0;
    var jiweiNum = that.data.jiweiNum;
    var singleprice = that.data.singleprice;
    var zotalprice = 0;
    if(flag == false){
      index++;
      jiweiNum.push(index);
      jiweiNum = that.uniq(jiweiNum);
      // 机位的增加
        num = that.data.jiweiNum.length;
        zotalprice = singleprice*num;
        that.setData({
          [img]: imglist1,
          [setflag]: true,
          lenarr: num,
          jiweiNum: jiweiNum,
          zotalprice: zotalprice
        })
    }else{
        index++;
        for(var i=0;i<jiweiNum.length;i++){
          if(index == jiweiNum[i]){
            var k = i;
            jiweiNum.splice(k,1);
          }
        }
        jiweiNum = that.uniq(jiweiNum);
        num = that.data.jiweiNum.length;
        zotalprice = singleprice*num;
        that.setData({
          [img]: imglist0,
          [setflag]: false,
          lenarr: num,
          jiweiNum: jiweiNum,
          zotalprice: zotalprice
        })
    }
    wx.setStorageSync('jiweiNum', jiweiNum);
  },
  releaseAircraft: function () {//发布拼机
    let that = this;

    let userId = wx.getStorageSync('userId');
    if (!userId) {
      app.goLoginPageTimeOut()
      return
    }
    if (!this.location){
      wx.showToast({
        title: '请选择出发地',
        duration: 1500
      });
      return
    } else if (!this.county){
      wx.showToast({
        title: '请选择目的地',
        duration: 1500
      });
      return
    } else if (!this.dateValue){
      wx.showToast({
        title: '请选择日期',
        duration: 1500
      });
      return
    }

    console.log(this.dateValue);
    let createdata={
      start_place_id: app.globalDatainfo.locationid,
      end_place_id: app.globalDatainfo.countyid,
      mudi: this.objectiveItemid,
      type:1,
      start_time: this.dateValue,
      uid: userId
    };
      wx.request({
        url: app.api.kzdsurl + app.interface.pinji_create,
        method: 'get',
        data: createdata,
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          console.log(res)
          //that.initProductData(data);
          wx.navigateBack();
          //endInitData
        },
        fail: function (e) {
          wx.showToast({
            title: '网络异常！',
            duration: 2000
          });
        },
      });

  },
  switchObject: function (e) {//选择拼机目的
    this.objectiveItemid = e.target.id;
    this.setData({
      objectiveItemid: e.target.id
    })

  },
  bindDateChange: function (e) {
    this.dateValue = e.detail.value;
    this.setData({
      dateValue: e.detail.value
    })
  },
  baoji_list: function () { //
    var that = this;
    wx.request({
      url: app.api.kzdsurl + app.interface.baojiji_list,
      method: 'get',
      data: {},
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        that.setData({
          baoji_list: res.data.data,

        });
        //endInitData
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      },
    });
  },
  onShareAppMessage: function () {
    return {
      title: '宠物美容学校',
      path: '/pages/index/index',
      success: function (res) {
        // 分享成功
      },
      fail: function (res) {
        // 分享失败
      }
    }
  }



});